package com.tsj.spzx.service.Impl;

import cn.hutool.core.date.DateUtil;
import com.tsj.spzx.mapper.OrderStatisticsMapper;
import com.tsj.spzx.model.dto.order.OrderStatisticsDto;
import com.tsj.spzx.model.entity.order.OrderStatistics;
import com.tsj.spzx.model.vo.order.OrderStatisticsVo;
import com.tsj.spzx.service.OrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderInfoServiceImpl implements OrderInfoService {

    @Autowired
    private OrderStatisticsMapper orderStatisticsMapper;

    /**
     * 根据所选日期区间，返回订单相应信息
     * @param orderStatisticsDto 包含开始时间 和 结束时间
     * @return
     */
    @Override
    public OrderStatisticsVo getOrderStatisticsData(OrderStatisticsDto orderStatisticsDto) {
        // 1、现根据日期区间，查询订单信息
        List<OrderStatistics> orderStatisticsList = orderStatisticsMapper.getOrderStatisticsData(orderStatisticsDto);
        // 2、获取日期集合
        List<String> dateList = orderStatisticsList.stream().map(orderStatistics ->
                DateUtil.format(orderStatistics.getOrderDate(), "yyyy-MM-dd")).collect(Collectors.toList());
        // 3、获取销售额集合
        List<BigDecimal> moneyList = orderStatisticsList.stream().map(
                OrderStatistics::getTotalAmount).collect(Collectors.toList());

        OrderStatisticsVo orderStatisticsVo = new OrderStatisticsVo();
        orderStatisticsVo.setDateList(dateList);
        orderStatisticsVo.setAmountList(moneyList);
        return orderStatisticsVo;
    }
}
